package lib.hj.com.commonlib.utils;


import com.google.gson.Gson;
import com.orhanobut.logger.Logger;

import lib.hj.com.commonlib.BuildConfig;
import lib.hj.com.commonlib.bean.BaseBean;
import lib.hj.com.commonlib.bean.ExceptionBean;
import lib.hj.com.commonlib.manager.HttpManager;
import okhttp3.Call;
import okhttp3.Request;
import okhttp3.Response;


/**
 * author:dyz
 * 网络请求底层回调
 * Created by Cola on 15/10/8.
 */

public  class Callback extends com.zhy.http.okhttp.callback.Callback{


    Req req;
    String parameter = "";
    Gson gson = new Gson();


    public  Callback(Req req) {
        this.req = req;

        if (req.parameter != null)
        {
            if (req.parameter.get("data") != null) {

                parameter = req.parameter.get("data").toString();

            } else {
                parameter = "";
            }
        }

    }

    @Override
    public BaseBean parseNetworkResponse(Response response, int id) throws Exception {

        String stringTemp = response.body().string();
        HttpManager.myLog(req.url+"参数----->",req.postData);
        HttpManager.myLog(req.url+"返回----->",stringTemp);



        String string = "";
        try {
            int start = stringTemp.indexOf("{");
            int end = stringTemp.lastIndexOf("}");
            string = stringTemp.substring(start, end+1);
        } catch (Exception e) {
            string = stringTemp;
        }

        Request request = response.request();
        BaseBean baseBean = new BaseBean();
        try {
            if (req.url.contains(Constant.save)) {
                if (string.contains("EXCEPTION")) {
                    ExceptionBean exceptionBean = new ExceptionBean();
                    exceptionBean.setMessage("标签已被使用");
                    baseBean.setEXCEPTION(exceptionBean);
                    baseBean.setSuccess(0);
                } else {
                    baseBean.setSuccess(1);
                }
            } else {
                baseBean = (BaseBean) gson.fromJson(string, req.cls);
            }

        } catch (Exception e) {
            baseBean = new BaseBean();
            if (req.url.contains(Constant.save)) {

                if (string.contains("EXCEPTION")) {
                    ExceptionBean exceptionBean = new ExceptionBean();
                    exceptionBean.setMessage("标签已被使用");
                    baseBean.setEXCEPTION(exceptionBean);
                    baseBean.setSuccess(0);
                } else {
                    baseBean.setSuccess(1);
                }



            } else {


                baseBean.setSuccess(-1000);
            }


        }

        baseBean.setResult(string);
        baseBean.setUrl(request.url().toString());
        baseBean.setParameter(req.postData);
        baseBean.setKey(req.key);

        return baseBean;
    }

    @Override
    public void onError(Call call, Exception e, int id) {

        HttpManager.myLog(req.url+"参数----->",req.postData);
        if (e != null ) {
            HttpManager.myLog("出错信息",e.toString());
        }

        if (req.onAsynHttp != null) {

            if (req.url.contains("/pdxx/loadpddwp.jsp")||req.url.contains("/pdxx/loadpdd.jsp")||(req.url.contains("/aj/get.jsp")||req.url.contains("/cwcx/get.jsp")  || req.url.contains("Yhxx/load.jsp") || req.url.contains("wp/savephoto.jsp")) && BuildConfig.HJDebug) {
                //          if (req.url.contains("Yhxx/load.jsp") && BuildConfig.HJDebug){

                moni(req.url);


            }else
            {
                req.onAsynHttp.onFailure(404,"网络出错");
                boolean isHas = false;
                for (int i = 0; i < Constant.offLines.length; i++) {

                    String offline = Constant.offLines[i];

                    if (req.url.contains(offline)) {

                        isHas = true;
                        break;

                    }

                }
                if (isHas) {
                    req.onAsynHttp.offLine(req);
                }
                if (req.isMoni && BuildConfig.HJDebug) {

                    String moni = "{'code':'0'}";
                    BaseBean baseBean = (BaseBean) gson.fromJson(moni, req.cls);
                    baseBean.setResult(moni);
                    baseBean.setParameter(req.postData);
                    baseBean.setKey(req.key);
                    req.onAsynHttp.onSuccess(baseBean);

                }

            }

        }



    }

    @Override
    public void onResponse(Object response, int id) {


        if (req.onAsynHttp != null) {
            BaseBean baseBean = (BaseBean) response;
            if (baseBean.success == 1 || baseBean.getSUCCESS() == 1 ) {

                req.onAsynHttp.onSuccess((BaseBean) response);

            } else {
                if (baseBean.success == -1000) {
                    req.onAsynHttp.onFailure(1001, baseBean.getResult());
                } else {

                    try {
                        req.onAsynHttp.onFailure(1001, baseBean.toString());
                    } catch (Exception e) {
                        req.onAsynHttp.onFailure(1001, "");

                    }
                }
            }

        }


    }

    public  void  moni(String url)
    {

        if (url.contains("Yhxx/load.jsp")) {

            String string = "";
            if (parameter.contains("呵呵")) {

                string = "{\"DATA\":[{\"yhm\":\"林东耀\",\"dwmc\":\"广东省公安厅机场公安局白云机场航站区派出所\",\"dwdm\":\"440000261000\",\"js\":\"办案民警\"}],\"success\":\"1\"}\n";
            } else {
                string = "{\"DATA\":[{\"yhm\":\"林东耀\",\"dwmc\":\"广东省公安厅机场公安局白云机场航站区派出所\",\"dwdm\":\"440000261000\",\"js\":\"仓管员\"}],\"success\":\"1\"}\n";

            }
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);


            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }

        if (url.contains("wp/savephoto.jsp")) {
            req.onAsynHttp.onSuccess(new BaseBean());

        }


        if (url.contains("/pdxx/save.jsp")) {
            String string = "{\"success\":\"1\"}";
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);


            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }
        if (url.contains("/wp/saverfid.jsp")) {
            String string = "{\"success\":\"1\"}";
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);


            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }

        if (url.contains("/cwcx/get.jsp")) {
            String string = "{\n" +
                    "    \"DATA\": [\n" +
                    "        {\n" +
                    "            \"AJBH\": \"A4401115400002016020008\",\n" +
                    "            \"WPBH\": \"警宗物品编号\",\n" +
                    "            \"CREATEDTIME\": \"物品采集时间\",\n" +
                    "            \"TXM\": \"123456789\",\n" +
                    "            \"WPXXMC\": \"物品详细名称1\",\n" +
                    "            \"WPSL\": \"100\",\n" +
                    "            \"TZMS\": \"特征描述\"\n" +
                    "        },\n" +
                    "        {\n" +
                    "            \"AJBH\": \"A4401115400002016020008\",\n" +
                    "            \"WPBH\": \"警宗物品编号\",\n" +
                    "            \"CREATEDTIME\": \"物品采集时间\",\n" +
                    "            \"TXM\": \"12358469514645\",\n" +
                    "            \"WPXXMC\": \"物品详细名称2\",\n" +
                    "            \"WPSL\": \"10000\",\n" +
                    "            \"TZMS\": \"特征描述\"\n" +
                    "        }\n" +
                    "    ],\n" +
                    "    \"success\": \"1\"\n" +
                    "}";
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);


            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }

        if (url.contains("searchService/aj/get.jsp")) {
            String string = "{\n" +
                    "    \"DATA\": [\n" +
                    "        {\n" +
                    "            \"AJBH\": \"J440111540000201512000024\",\n" +
                    "            \"AJLX\": \"01\",\n" +
                    "            \"AJSTATE\": \"02\",\n" +
                    "            \"AJMC\": \"李四盗窃案\",\n" +
                    "            \"AB\": \"020101\",\n" +
                    "            \"SL_BJSLH\": \"J440111540000201001000003\",\n" +
                    "            \"SLFXRQ\": \"2015-12-11 17:28:00\",\n" +
                    "            \"FASJCZ\": \"2015-12-11 17:28:00\",\n" +
                    "            \"FASJZZ\": \"2015-12-11 17:28:00\",\n" +
                    "            \"FADD\": \"广东省广州市白云区永平街永泰解放庄一巷1号\",\n" +
                    "            \"ZARS\": \"5\",\n" +
                    "            \"ZAGJ\": \"A0106,A0106\",\n" +
                    "            \"SWRS\": \"2\",\n" +
                    "            \"SSRS\": \"1\",\n" +
                    "            \"SSJZ\": \"100.00\",\n" +
                    "            \"ZYAQ\": \"主要案情举例\",\n" +
                    "            \"SLJSDW\": \"440111540000\",\n" +
                    "            \"RESERVATION01\": \"王五\",\n" +
                    "            \"ZBDW\": \"440111540000\"\n" +
                    "        }\n" +
                    "    ],\n" +
                    "    \"success\": \"1\"\n" +
                    "}";
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);
            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }

        if (url.contains("/pdxx/loadpdd.jsp")) {
            String string = "{\n" +
                    "    \"DATA\": [\n" +
                    "        {\n" +
                    "            \"PDDH\": \"123456789098765\",\n" +
                    "            \"PDCK\": \"PCS12345676543456\",\n" +
                    "            \"PDRQ\": \"2015-12-11 17:28:00\",\n" +
                    "            \"PDDW\": \"440111540000\",\n" +
                    "            \"PDE\": \"sylmj\",\n" +
                    "            \"RKSJ\": \"2015-12-1117: 28: 00\"\n" +
                    "        }\n" +
                    "    ],\n" +
                    "    \"success\": \"1\"\n" +
                    "}";
            Logger.e(string);

            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);
            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }
        if (req.url.contains("/pdxx/loadpddwp.jsp")) {
            String string = "{\n" +
                    "    \"DATA\": [\n" +
                    "        {\n" +
                    "            \"XH\": \"1\",\n" +
                    "            \"WPBH\": \"PCS12345676543456\",\n" +
                    "            \"WPLB\": \"01\",\n" +
                    "            \"WPMC\": \"A0001\",\n" +
                    "            \"WPXXMC\": \"物品详细名称1\",\n" +
                    "            \"KCSL\": \"100\",\n" +
                    "            \"TXM\": \"1234567654345\",\n" +
                    "            \"KW\": \"0001\",\n" +
                    "            \"CREATEDTIME\": \"物品采集时间\",\n" +
                    "            \"TZMS\": \"特征描述\"\n" +
                    "        }\n" +
                    "    ],\n" +
                    "    \"success\": \"1\"\n" +
                    "}";
            Logger.e(string);
            BaseBean baseBean = (BaseBean) gson.fromJson(string, req.cls);
            baseBean.setResult(string);

            method1(req.url);
            method1(parameter);
            method1(string);
            req.onAsynHttp.onSuccess((BaseBean) baseBean);
        }


        Logger.e(parameter);
    }


    /**
     * 追加文件：使用FileOutputStream，在构造FileOutputStream时，把第二个参数设为true
     */
    public static void method1( String conent) {
//        BufferedWriter out = null;
//        try {
//            out = new BufferedWriter(new OutputStreamWriter(
//                    new FileOutputStream("", true)));
//            out.write(conent);
//
//        } catch (Exception e) {
//            e.printStackTrace();
//        } finally {
//            try {
//                out.write("\r\n");
//
//                out.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
    }


}
